package xujiwen.transport.Mgr;

import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.User;
import xujiwen.transport.util.DBConnect;

public class UserMgr {
	
	public void UpdatePassword(final int uid, final String password) {
		final DBConnect db = new DBConnect();

		final String sql = "update USER set PASSWORD=?"
				+ "where UID = " + Integer.toString(uid);
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, password);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	/**
	 * @brief 判断用户密码是否正确
	 * @param password 旧密码 uid 用户编号
	 * @return true 正确 false 错误
	 */
	public boolean IsPasswordExsit(final String password, final int uid) {
		final DBConnect db = new DBConnect();
		final String sql = "select count(*) from USER where UID=? and PASSWORD=?";
		//System.out.println(sql);
		
		db.prepareStatement(sql);
		
		db.setInt(1, uid);
		db.setString(2, password);
		int userRecord = 0;
		db.excuteQuery();
		try {
			while(db.next())
			{
				userRecord = db.getInt(1);
			}
		}catch (final Exception ex) {
			ex.printStackTrace();
			//return null;
		} finally {
			db.close();
		}
		if(userRecord > 0)
			return true;
		else
			return false;
	}
	public void Save(final User user) {
		// 默认新加用户为操作员
		user.setUclass(1);
		final DBConnect db = new DBConnect();
		final String sql = "insert into USER values (null, ?, ?, ?, now(), ?)";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			final String name = user.getName();
			final String password = user.getPassword();
			final Integer uclass = user.getUclass();
			final String sex = user.getSex();
			db.setString(1, name);
			db.setString(2, password);
			db.setInt(3, uclass.intValue());
			db.setString(4, sex);
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	public User GetDeatilUser(final int uid) {
		User user = null;
		final DBConnect db = new DBConnect();
		final String sql = "select * from USER where UID = ?";
		db.prepareStatement(sql);
		db.setInt(1, uid);
		db.excuteQuery();
		try {
			while (db.next()) {
				user = new User();
				user.setUid(db.getInt("UID"));
				user.setName(db.getString("NAME"));
				user.setPassword(db.getString("PASSWORD"));
				user.setUclass(db.getInt("CLASS"));
				user.setSex(db.getString("SEX"));
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return user;
	}
	
	public void UpdateLoginTime(final int uid) {
		final DBConnect db = new DBConnect();
		final String sql = "update USER set LASTLOGIN=now() where UID=?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, uid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	public void UpdateUclass(final int uid, final int uclass) {
		final DBConnect db = new DBConnect();
		final String sql = "update USER set CLASS=? where UID=?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, uclass);
			db.setInt(2, uid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	public List<User> GetSearchUserList(String str) {
		final List<User> users = new ArrayList<User>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select * from USER where NAME like '%" + str +"%' or UID like '%" + str +"%'";
		else sql = "select * from USER";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final User user = new User();
				user.setUid(db.getInt("UID"));
				user.setName(db.getString("NAME"));
				user.setPassword(db.getString("PASSWORD"));
				user.setUclass(db.getInt("CLASS"));
				user.setSex(db.getString("SEX"));
				user.setLastLogin(db.getTimestamp("LASTLOGIN"));
				users.add(user);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return users;
	}
	public void Del(final int uid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from USER where UID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, uid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
}
